import VuiClipboardDataItem from './data-item';

/*
 *
 * @Author: Jevon
 * @Date: 2021-12-16 10:55:57
 */
const VuiClipboardCommandUtils = (function() {
  /**
   * 检查是否支持
   * @param {*}
   * @return {*}
   */
  function check() {
    return true;
  }
  /**
   * 向剪切板写入文本
   * @param {*} data
   * @return {*}
   */
  function writeText(data) {
    return new Promise((resolve, reject) => {
      try {
        const fakeElement = createFakeElement(data);
        document.body.appendChild(fakeElement);
        const selectedText = select(fakeElement);
        command('copy');
        fakeElement.remove();
        resolve(selectedText);
      } catch (error) {
        reject('write error');
      }
    });
  }
  /**
   * 读取剪切板中的文本
   * @param {*}
   * @return {*}
   */
  function readText() {
    return new Promise((resolve, reject) => {
      try {
        const fakeElement = createFakeElement(data);
        document.body.appendChild(fakeElement);
        command('paste');
        const selectedText = select(fakeElement);
        fakeElement.remove();
        resolve(selectedText);
      } catch (error) {
        reject('read error');
      }
    });
  }
  /**
   * 向剪切板写入内容
   *
   *  data = new VuiClipboardDataItem({
      'text/plain': text,
      'image/png': image
      }
   * @param { VuiClipboardDataItem } data
   * @return {*}
   */
  function write(data) {
    return writeText(data.getText());
  }
  /**
   * 根据类型读取剪切板
   * @param {*} format
   * @return {*}
   */
  function read(format) {
    return readText();
  }

  return {
    check,
    writeText,
    readText,
    write,
    read
  };
})();
export default VuiClipboardCommandUtils;
